package com.neusoft.neuiotms.auth.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import com.neusoft.bizcore.auth.dto.AuthRoleDTO;
import com.neusoft.bizcore.auth.service.AuthRoleService;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.common.constant.AHConstants;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api("角色管理")
@RestController
@RequestMapping(value = "/api/w/auth/roles")
public class AuthRoleExController {

    @Autowired
    private AuthRoleService authRoleService;

    @ApiOperation("查询所有角色")
    @GetMapping("/ex")
    public ResultListDTO<AuthRoleDTO> index(final Searchable searchable) {
        if (searchable.hasKey("belongs")) {
            final String belongs = searchable.getStrValue("belongs");
            if (AHConstants.BELONG_KJ_CODE.equals(belongs)) {
                final List<String> slRoles = Lists.newArrayList(AHConstants.ROLE_KJ_ADMINISTRATION_PRINCIPAL,
                        AHConstants.ROLE_KJ_ASSET_ADMIN, AHConstants.ROLE_KJ_DEPT_ASSET_ADMIN,
                        AHConstants.ROLE_KJ_DEPT_PRINCIPAL, AHConstants.ROLE_KJ_SYSTEM_USER);
                searchable.put("roleCodeArray", slRoles);
            } else if (AHConstants.BELONG_SL_CODE.equals(belongs)) {
                final List<String> slRoles = Lists.newArrayList(AHConstants.ROLE_SL_ASSET_BUYER,
                        AHConstants.ROLE_SL_ASSET_OWNER_ADMIN, AHConstants.ROLE_SL_ASSET_OWNER_PRINCIPAL,
                        AHConstants.ROLE_SL_DEPT_ASSET_ADMIN, AHConstants.ROLE_SL_DEPT_ASSET_PRINCIPAL);
                searchable.put("roleCodeArray", slRoles);
            }
        }

        return ResultListDTO.success(this.authRoleService.search(searchable));
    }

    @ApiOperation("分页查询所有角色")
    @GetMapping("/ex/p")
    public PageResultDTO<AuthRoleDTO> search(final Pageable pageable, final Searchable searchable) {
        if (searchable.hasKey("belongs")) {
            final String belongs = searchable.getStrValue("belongs");
            if (AHConstants.BELONG_KJ_CODE.equals(belongs)) {
                final List<String> slRoles = Lists.newArrayList(AHConstants.ROLE_KJ_ADMINISTRATION_PRINCIPAL,
                        AHConstants.ROLE_KJ_ASSET_ADMIN, AHConstants.ROLE_KJ_DEPT_ASSET_ADMIN,
                        AHConstants.ROLE_KJ_DEPT_PRINCIPAL, AHConstants.ROLE_KJ_SYSTEM_USER);
                searchable.put("roleCodeArray", slRoles);
            } else if (AHConstants.BELONG_SL_CODE.equals(belongs)) {
                final List<String> slRoles = Lists.newArrayList(AHConstants.ROLE_SL_ASSET_BUYER,
                        AHConstants.ROLE_SL_ASSET_OWNER_ADMIN, AHConstants.ROLE_SL_ASSET_OWNER_PRINCIPAL,
                        AHConstants.ROLE_SL_DEPT_ASSET_ADMIN, AHConstants.ROLE_SL_DEPT_ASSET_PRINCIPAL);
                searchable.put("roleCodeArray", slRoles);
            }
        }
        return PageResultDTO.success(this.authRoleService.search(pageable, searchable));
    }

}
